package com.example.experimen2;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private final static String TAG = "MainActivity";
    private TextView tvScore;
    private TextView tvTime;
    private ImageView ivRefresh;
    private GameAreaView gameAreaView;

    private final int rowCount = 6;
    private final int columnCount = 6;

    public static int totalTime = 120 * 1000;

    public CountDownTimer countDownTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //绑定控件
        tvScore = findViewById(R.id.tv_score);
        tvTime = findViewById(R.id.tv_time);
        ivRefresh = findViewById(R.id.iv_refresh);
        //为刷新按钮添加单击监听事件
        ivRefresh.setOnClickListener(v -> {
            gameAreaView.removeAllViewsInLayout();
            restartGame();
        });

        gameAreaView = findViewById(R.id.gv_game_board);
        //初始化游戏区水果数量：4x4表格
        gameAreaView.setRowCount(rowCount);
        gameAreaView.setColumnCount(columnCount);

        //初始化计时器
        countDownTimer = new CountDownTimer(totalTime, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvTime.setText(String.valueOf(millisUntilFinished / 1000));
            }

            @Override
            public void onFinish() {
                gameLose();
            }
        };
        startGame();
    }

    private void startGame() {
        //开始游戏
        gameAreaView.startGame(this);
        //倒数计时
        countDownTimer.start();
    }

    public void restartGame() {
        //重新开始游戏
        gameAreaView.stopGame();
        gameAreaView.refreshGameBoard();

        countDownTimer.cancel();//先取消旧的，再重新开始
        countDownTimer.start();
    }

    public void updateScore(int score) {
        if (score != 0) {
            int totalScore = Integer.parseInt(tvScore.getText().toString()) + score;
            tvScore.setText(String.valueOf(totalScore));
        }
    }

    public void gameWin() {
        int passTime = totalTime / 1000 - Integer.parseInt(tvTime.getText().toString());
        //游戏成功，显示AlertDialog
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("胜利！");
        builder.setMessage("用时：" + passTime + "秒\n分数：" + tvScore.getText().toString());
        builder.setPositiveButton("重来", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                restartGame();
            }
        });
        builder.setNegativeButton("退出", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                MainActivity.this.finish();
            }
        });
        builder.create().show();
        countDownTimer.cancel();
    }

    public void gameLose() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("失败！");
        builder.setMessage("游戏失败");
        builder.setPositiveButton("重来", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                gameAreaView.removeAllViewsInLayout();
                restartGame();
            }
        });
        builder.setNegativeButton("退出", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                MainActivity.this.finish();
            }
        });
        builder.create().show();
        countDownTimer.cancel();

        gameAreaView.stopGame();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GameSound.getInstance(this).release();
    }
}